package com.example.pkmedical.Activity.weight.tozero;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Activity.servicecode.CalibrationSensorActivity;
import com.example.pkmedical.Activity.servicecode.SensorSuccessActivity;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.SerialProtUtil;
import com.example.pkmedical.Utils.TestTime;
import com.example.pkmedical.Utils.TimeUtils;
import com.example.pkmedical.Utils.ToastUtils;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

import static com.example.pkmedical.Utils.TimeUtils.getCurrentTime;

public class ReleaseZeroActivity extends BaseActivity {

    @BindView(R.id.releasezero_onetext)
    TextView releasezeroOnetext;
    @BindView(R.id.releasezero_twotext)
    TextView releasezeroTwotext;
    Button releasezeroButton;
    @BindView(R.id.releasezero_title_text)
    TextView releasezeroTitleText;
    private static final String TAG = "yangshuo";
    byte[] mBytes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_releasezero);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        TimeCount();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick(R.id.releasezero_button)
    public void onViewClicked() {
    }

    private int timeLong = 4;
    private Disposable mTimeCountDisposable;

    /**
     * 倒计时
     */
    public void TimeCount() {
        if (mTimeCountDisposable != null) {
            //取消上一次的订阅
            mTimeCountDisposable.dispose();
        }
//        //0-count依次输出，延时0s执行，每1s发射一次。
        mTimeCountDisposable = Flowable.intervalRange(4, 4, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(aLong -> {
                    timeLong -= 1;
                    releasezeroTwotext.setText(timeLong + "秒内归零称重器");
                })
                .doOnComplete(() -> {
                    if (mTimeCountDisposable != null) {
                        mTimeCountDisposable.dispose();
                    }
                    startActivity(new Intent(MyApp.getAppContext(), ReleaseZeroNextActivity.class));
                    ReleaseZeroActivity.this.finish();
                })
                .subscribe();
    }
}
